package com.rayo.keyboxdemo;

import com.rayo.wallreader.data.AuxiliaryKeyInfo;
import com.rayo.wallreader.data.BlackListKeyInfo;
import com.rayo.wallreader.data.KeyBasicInfo;
import com.rayo.wallreader.data.KeyEventInfo;
import com.rayo.wallreader.data.KeyType;
import com.rayo.wallreader.data.SettingKeyInfo;
import com.rayo.wallreader.data.SystemCodeKeyInfo;
import com.rayo.wallreader.data.UserKeyInfo;
import com.rayo.wallreader.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by yanghaojie on 16/7/12.
 */
public class KeyDefineData {
    private KeyBasicInfo mKeyBasicInfo;

    private UserKeyInfo mUserKeyInfo;

    private AuxiliaryKeyInfo mAuxiliaryKeyInfo;

    private BlackListKeyInfo mBlackListKeyInfo;

    private SettingKeyInfo mSettingKeyInfo;

    private SystemCodeKeyInfo mSystemCodeKeyInfo;

    private KeyEventInfo mKeyEventInfo;

    public KeyDefineData() {
        this.newKeyBasicInfo();
        this.newUserKeyInfo();
        this.newKeyEventInfo();
    }

    public KeyBasicInfo newKeyBasicInfo() {
        mKeyBasicInfo = new KeyBasicInfo();
//        mKeyBasicInfo.setSysCode(new byte[]{0x11, 0x11, (byte)0x83, (byte)0xfe});
//        mKeyBasicInfo.setRegCode(new byte[]{0x11, 0x11, 0x0, 0x0});
        return mKeyBasicInfo;
    }

    public KeyBasicInfo getmKeyBasicInfo() {
        return mKeyBasicInfo;
    }

    public SettingKeyInfo newSettingKey() {
        mKeyBasicInfo.setKeyId(1);
        mKeyBasicInfo.setType(KeyType.SETTING_KEY.toInteger());
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(Calendar
                .getInstance()));
        Calendar nowtime = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(calendar));
        mKeyBasicInfo.setKeyIssueTime(TimeUtils.dateToBytesYMDHM(calendar.getTime()));
        mKeyBasicInfo.setBeginTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        nowtime.add(Calendar.HOUR_OF_DAY, 2);
        mKeyBasicInfo.setEndTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        mSettingKeyInfo = new SettingKeyInfo();
        mSettingKeyInfo.setLockId(1);
        mSettingKeyInfo.setGroupId(2);
        return mSettingKeyInfo;
    }

    public SystemCodeKeyInfo newSystemCodeKey() {
        mKeyBasicInfo.setKeyId(1);
        mKeyBasicInfo.setType(KeyType.SYSCODE_KEY.toInteger());
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(Calendar
                .getInstance()));
        Calendar nowtime = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(calendar));
        mKeyBasicInfo.setKeyIssueTime(TimeUtils.dateToBytesYMDHM(calendar.getTime()));
        mKeyBasicInfo.setBeginTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        nowtime.add(Calendar.HOUR_OF_DAY, 2);
        mKeyBasicInfo.setEndTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));

        mSystemCodeKeyInfo = new SystemCodeKeyInfo();
        return mSystemCodeKeyInfo;
    }

    public BlackListKeyInfo newBlackListKey() {
        mKeyBasicInfo.setKeyId(1);
        mKeyBasicInfo.setType(KeyType.LOSS_KEY.toInteger());
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(Calendar
                .getInstance()));
        Calendar nowtime = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(calendar));
        mKeyBasicInfo.setKeyIssueTime(TimeUtils.dateToBytesYMDHM(calendar.getTime()));
        mKeyBasicInfo.setBeginTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        nowtime.add(Calendar.HOUR_OF_DAY, 2);
        mKeyBasicInfo.setEndTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));

        mBlackListKeyInfo = new BlackListKeyInfo();
        byte[] blacklist = {0x1, 0x0, 0x2, 0x0, 0x3, 0x0, 0x4, 0x0, 0x5, 0x0,
                0x6, 0x0, 0x7, 0x0, 0x8, 0x0, 0x9, 0x0, 0xa, 0x0};
        mBlackListKeyInfo.setBlackList(blacklist);
        return mBlackListKeyInfo;
    }

    public KeyBasicInfo newEventKey() {
        mKeyBasicInfo.setKeyId(1);
        mKeyBasicInfo.setType(KeyType.EVENT_KEY.toInteger());
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(Calendar
                .getInstance()));

        Calendar nowtime = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(calendar));
        mKeyBasicInfo.setKeyIssueTime(TimeUtils.dateToBytesYMDHM(calendar.getTime()));
        mKeyBasicInfo.setBeginTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        nowtime.add(Calendar.HOUR_OF_DAY, 2);
        mKeyBasicInfo.setEndTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));

        return mKeyBasicInfo;
    }

    public KeyBasicInfo newEngineeringKey() {
        mKeyBasicInfo.setKeyId(1);
        mKeyBasicInfo.setType(KeyType.ENG_KEY.toInteger());
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(Calendar
                .getInstance()));
        Calendar nowtime = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(calendar));
        mKeyBasicInfo.setKeyIssueTime(TimeUtils.dateToBytesYMDHM(calendar.getTime()));
        mKeyBasicInfo.setBeginTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        nowtime.add(Calendar.HOUR_OF_DAY, 2);
        mKeyBasicInfo.setEndTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));

        return mKeyBasicInfo;
    }

    public KeyBasicInfo newEmergencyKey() {
        mKeyBasicInfo.setKeyId(1);
        mKeyBasicInfo.setType(KeyType.EMERGENCY_KEY.toInteger());
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(Calendar
                .getInstance()));
        Calendar nowtime = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(calendar));
        mKeyBasicInfo.setKeyIssueTime(TimeUtils.dateToBytesYMDHM(calendar.getTime()));
        mKeyBasicInfo.setBeginTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        nowtime.add(Calendar.HOUR_OF_DAY, 2);
        mKeyBasicInfo.setEndTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));

        return mKeyBasicInfo;
    }

    public KeyBasicInfo newReadLockIdKey() {
        mKeyBasicInfo.setKeyId(1);
        mKeyBasicInfo.setType(KeyType.READLOCKID_KEY.toInteger());
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(Calendar
                .getInstance()));
        Calendar nowtime = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(calendar));
        mKeyBasicInfo.setKeyIssueTime(TimeUtils.dateToBytesYMDHM(calendar.getTime()));
        mKeyBasicInfo.setBeginTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        nowtime.add(Calendar.HOUR_OF_DAY, 2);
        mKeyBasicInfo.setEndTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));

        return mKeyBasicInfo;
    }

    public KeyBasicInfo newReadSerialKey() {
        mKeyBasicInfo.setKeyId(1);
        mKeyBasicInfo.setType(KeyType.READLOCKNUM_KEY.toInteger());
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(Calendar
                .getInstance()));
        Calendar nowtime = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(calendar));
        mKeyBasicInfo.setKeyIssueTime(TimeUtils.dateToBytesYMDHM(calendar.getTime()));
        mKeyBasicInfo.setBeginTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        nowtime.add(Calendar.HOUR_OF_DAY, 2);
        mKeyBasicInfo.setEndTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        return mKeyBasicInfo;
    }

    public UserKeyInfo newBlankUserKeyInfo() {
        return mUserKeyInfo = new UserKeyInfo();
    }

    public UserKeyInfo newUserKeyInfo() {
        Calendar nowtime = Calendar.getInstance();

        mKeyBasicInfo.setKeyId(2);
        mKeyBasicInfo.setType(KeyType.USER_KEY.toByte());

        Calendar calendar = Calendar.getInstance();
        mKeyBasicInfo.setKeyTime(TimeUtils.calToBytesYMDWHMS(calendar));
        mKeyBasicInfo.setKeyIssueTime(TimeUtils.dateToBytesYMDHM(calendar.getTime()));
        mKeyBasicInfo.setBeginTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        nowtime.add(Calendar.HOUR_OF_DAY, 2);
        mKeyBasicInfo.setEndTime(TimeUtils.dateToBytesYMDHM(nowtime.getTime()));
        mKeyBasicInfo.setVerifyDay(0);
        mUserKeyInfo = new UserKeyInfo();

//        // Calendar
        List<UserKeyInfo.CalendarInfo> CalList = new ArrayList<>();
        for (int cal = 0; cal < 1; cal++) {
            UserKeyInfo.CalendarInfo calday = new UserKeyInfo.CalendarInfo();
            calday.CalDate = Calendar.getInstance();
            calday.CalDate.add(Calendar.DATE, cal + 10);
            calday.Type = cal % 4;
            CalList.add(calday);
        }
        mUserKeyInfo.setCalendarInfo(CalList);
//
        // DaylightSaving
        UserKeyInfo.DSTInfo dst = new UserKeyInfo.DSTInfo();
        dst.BeginDate = Calendar.getInstance();
        dst.EndDate = Calendar.getInstance();
        dst.EndDate.add(Calendar.MONTH, 6);
        mUserKeyInfo.setDST(dst);

//         Timezone
//         Timezone MAX：200；Time MAX：12
        List<UserKeyInfo.TimezoneInfo> timeList = new ArrayList<UserKeyInfo.TimezoneInfo>();
        List<UserKeyInfo.ZoneInfo> zoneList = new ArrayList<UserKeyInfo.ZoneInfo>();
        for (int z = 0; z < 3; z++) {
            for (int t = 0; t < 12; t++) {
                UserKeyInfo.TimezoneInfo time = new UserKeyInfo.TimezoneInfo();
                Date date = new Date(18, 5, 00, 00, 00);
                time.ZoneNo = z + 1;
                time.TimeNo = t + 1;
                time.From.setTime(date);
                date = new Date(25, 8, 19, 23, 59);
                time.To.setTime(date);
                time.Su = true;
                time.Mo = true;
                time.Tu = true;
                time.We = true;
                time.Th = true;
                time.Fr = true;
                time.Sa = true;
                time.Hol = true;
                time.Sp1 = true;
                time.Sp2 = true;
                timeList.add(time);
            }
            UserKeyInfo.ZoneInfo zone = new UserKeyInfo.ZoneInfo();
            zone.ZoneNo = z + 1;
            zone.TimeCount = 12;
            zoneList.add(zone);
        }
        mUserKeyInfo.setTimezoneInfo(timeList);
        mUserKeyInfo.setZoneInfo(zoneList);
        mUserKeyInfo.setTimezoneCount(zoneList.size());

        //add edit open permission
        List<UserKeyInfo.Open> openList = new ArrayList<UserKeyInfo.Open>();
        for (int open = 0; open < 20; open++) {
            UserKeyInfo.Open opens = new UserKeyInfo.Open(UserKeyInfo.Open.LOCK_TYPE, open + 1, 1);
            openList.add(opens);
        }
        mUserKeyInfo.setOpens(openList);
//                 TempOpens MAX：100
        List<UserKeyInfo.TempOpen> tempList = new ArrayList<UserKeyInfo.TempOpen>();
        for (int temp = 0; temp < 10; temp++) {
            UserKeyInfo.TempOpen topen = new UserKeyInfo.TempOpen();
            topen.EndDate.add(Calendar.MONTH, 2);
            topen.FromTime.set(Calendar.HOUR_OF_DAY, 8);
            topen.FromTime.set(Calendar.MINUTE, 30);
            topen.ToTime.set(Calendar.HOUR_OF_DAY, 22);
            topen.ToTime.set(Calendar.MINUTE, 30);
            topen.LockId = temp + 1;
            tempList.add(topen);
        }
        mUserKeyInfo.setTempOpens(tempList);
        return mUserKeyInfo;
    }

    public UserKeyInfo getUserKeyInfo() {
        return mUserKeyInfo;
    }

    public KeyEventInfo getmKeyEventInfo() {
        return mKeyEventInfo;
    }

    public KeyEventInfo newKeyEventInfo() {
        mKeyEventInfo = new KeyEventInfo();
        return mKeyEventInfo;
    }

    public KeyBasicInfo getKeyBasicInfo() {
        return mKeyBasicInfo;
    }

    public void setKeyBasicInfo(KeyBasicInfo keyBasicInfo) {
        mKeyBasicInfo = keyBasicInfo;
    }

    public void setUserKeyInfo(UserKeyInfo userKeyInfo) {
        mUserKeyInfo = userKeyInfo;
    }

    public AuxiliaryKeyInfo getAuxiliaryKeyInfo() {
        return mAuxiliaryKeyInfo;
    }

    public void setAuxiliaryKeyInfo(AuxiliaryKeyInfo auxiliaryKeyInfo) {
        mAuxiliaryKeyInfo = auxiliaryKeyInfo;
    }

    public BlackListKeyInfo getBlackListKeyInfo() {
        return mBlackListKeyInfo;
    }

    public void setBlackListKeyInfo(BlackListKeyInfo blackListKeyInfo) {
        mBlackListKeyInfo = blackListKeyInfo;
    }

    public SettingKeyInfo getSettingKeyInfo() {
        return mSettingKeyInfo;
    }

    public void setSettingKeyInfo(SettingKeyInfo settingKeyInfo) {
        mSettingKeyInfo = settingKeyInfo;
    }

    public SystemCodeKeyInfo getSystemCodeKeyInfo() {
        return mSystemCodeKeyInfo;
    }

    public void setSystemCodeKeyInfo(SystemCodeKeyInfo systemCodeKeyInfo) {
        mSystemCodeKeyInfo = systemCodeKeyInfo;
    }

    public KeyEventInfo getKeyEventInfo() {
        return mKeyEventInfo;
    }

    public void setKeyEventInfo(KeyEventInfo keyEventInfo) {
        mKeyEventInfo = keyEventInfo;
    }
}
